export const videoScroll = {
	
	pageShowKey:{
		index:"index",
		user:"user",
		circleDetail:"circleDetail",
		personalHomepage:"personalHomepage",
		dynamicDetail:"dynamicDetail",
		search:"search"
		
	},
	
	pageListOfShow:{
		
	},
	showPage(page){
		for(var key in this.pageListOfShow){
		 this.pageListOfShow[key]=false;
		  
		}
		this.pageListOfShow[page]=true; 
		console.log(this.pageListOfShow);
		
	},
	clearTime(){
		if(getApp().globalData.timeoutForVideo){
			clearInterval(getApp().globalData.timeoutForVideo);
		}
	},
	pauseVideo(){
		if(getApp().globalData.playingVideoContext) {
			getApp().globalData.playingVideoContext.pause;
		}
	},
	
	timein(_this, css, isNotAutoPlay) {
		//每次加载timein时候清空scrolltime 用于页面间的访问
		getApp().globalData.lastScrollTime=0;

		// ///处理tab下的问题, 例如index 没加载完立即点user 会导致 先加载user 再加载 index 这里要处理这2个页面
		// console.log(getCurrentPages(),_this.$scope.route,"getCurrentPages(4)");
		// let isTabPageCanPlay=true;
		// let pages = [];
		// for (var i = 0; i < getCurrentPages().length; i++) {
		// 	pages.push(getCurrentPages()[i].route);
		// }
		// if(pages.length==2&&pages.indexOf("pages/index/index")>=0&&pages.indexOf("pages/tabBar/user")>=0){
		// 	//第一个代表是后点击的 替换_this 为正确的_this
		// 	if(pages[0]=="pages/index/index"&&_this.$scope.route=="pages/tabBar/user"){
		//
		//
		// 	}
		//
		// 	if(pages[0]=="pages/tabBar/user"&&_this.$scope.route=="pages/index/index"){
		//
		//
		// 	}
		//
		//
		// }
		
		
		 
		var lastChangeVideoImageTimeList=Array.apply(null, Array(1000)).map(() => 0);
		this.clearTime();
		if (!css) {
			css = ".page-content"
		}
		getApp().globalData.timeoutForVideo = setInterval(() => {
			// console.log("setInterval10 ")
			// console.log(getApp().globalData.lastScrollTime,"setInterval video");
			_this.$nextTick(async () => {
				
				await new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(_this);
					let windowHeight = uni.getSystemInfoSync().windowHeight;
					let windowWidth = uni.getSystemInfoSync().windowWidth;
					let halfwindowHeight = windowHeight / 2;
					let selectcss = css;
					//#ifdef MP-BAIDU
					selectcss += ""
					//#endif
					//#ifndef MP-BAIDU
					selectcss += " >>> "
					//#endif
					selectcss +=' .videoimg_for_time_box';
					// console.log(selectcss,'selectcss')
					
					let queryselect = query.selectAll(selectcss);
					queryselect.boundingClientRect(data => {
						 // console.log("boundingClientRect " ,data)
						if (data && data.length > 0) {

							// 显示控制
							var i = 0;
							 
							
							 
							let isFirstPlaying =false;
							for ( i = 0; i < data.length; i++){
								if (data[i].dataset.id) {
									let itemIndex = data[i].dataset.index;
									

									// 是否在屏幕播放区域
									let isInPlayingArea= false;
									isInPlayingArea=!_this.isHide && data[i].top < halfwindowHeight && halfwindowHeight < data[i].top + data[i].height 

									//显示逻辑
									if(getApp().globalData.lastScrollTime!=0&&(getApp().globalData.lastScrollTime+300>new Date().getTime()||getApp().globalData.lastScrollTime+1100<new Date().getTime())){

										
									}else{
										//显示逻辑
										// 是否在屏幕播放区域
										// 一个模块的最小高度
										let oneMinBoxHeidth = windowWidth * 600 /750
										let isInShowArea=!_this.isHide && data[i].top -oneMinBoxHeidth < halfwindowHeight && halfwindowHeight < data[i].top + data[i].height + oneMinBoxHeidth;
										
										// isInShowArea=true;
										// if(isInShowArea)	console.log("in showvideo 1 61",itemIndex, isInShowArea)
										//当前
										if(isInShowArea!=data[i].dataset.isshowvideo){
											lastChangeVideoImageTimeList[itemIndex] = new Date().getTime();
											if(!isInShowArea){
												_this.setOutShowPause(itemIndex);
												
											}else{
												
											}
											_this.setVideoShow(itemIndex, isInShowArea);
											
											
											
											
										}
										
									}
									

									// console.log(getApp().globalData.lastScrollTime,i,isInPlayingArea+"=isInPlayingArea")


									//播放逻辑
									// console.log("!isNotAutoPlay" ,!isNotAutoPlay)

									if (!isNotAutoPlay) {
										if (isInPlayingArea) {
											if(lastChangeVideoImageTimeList[itemIndex] + 300 < new Date().getTime())
											_this.setPlayOrPause(itemIndex,true)
											// console.error(itemIndex,"play1 dodo ")
										} else {
											_this.setPlayOrPause(itemIndex,false)
											// console.error(itemIndex,"pause dodo ")

										}
									} else {
										if (isInPlayingArea) {
											if(lastChangeVideoImageTimeList[itemIndex] + 300 < new Date().getTime())
											_this.setPlayOrPause(itemIndex,false, true, 1)
											// console.log(itemIndex,"play")
										} else {
											_this.setPlayOrPause(itemIndex,false, true, 0)
												// console.log(itemIndex,"pause")
										}
									}
								}
							}
							// 播放控制

						}
					}).exec()
				})
			})
		}, 300)
	}
}
